#define _CRT_SECURE_NO_WARNINGS 1
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<unordered_map>
#include<queue>
#include<stack>
#include <functional>
#include <memory>
using namespace std;

class Solution {
public:
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int maxDistance(vector<vector<int>>& grid) {
        queue<pair<int, int>> qt;
        int n = grid.size(), m = grid[0].size();
        int flag = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == 1) qt.push({ i,j });
                else flag = 1;
            }
        }
        if (flag == 0) return -1;
        if (qt.size() == 0) return -1;
        while (qt.size())
        {
            auto [x, y] = qt.front();
            qt.pop();
            for (int i = 0; i < 4; i++)
            {
                int xi = x + dx[i];
                int yi = y + dy[i];
                if (xi >= 0 && xi < n && yi >= 0 && yi < m && grid[xi][yi] == 0)
                {
                    grid[xi][yi] = grid[x][y] + 1;
                    qt.push({ xi,yi });
                }
            }
        }
        int ret = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                ret = max(ret, grid[i][j]);
        return ret - 1;
    }
};

int main()
{
	return 0;
}